import bean.ExcelStudentData;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExcelStudentDataListener extends AnalysisEventListener<ExcelStudentData> {
    private static final int BATCH_COUNT = 5;
    List<ExcelStudentData> list = new ArrayList<>();
    @Override
    public void invoke(ExcelStudentData data, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", data);
        list.add(data);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            log.info("存数据库");
            // 存储完成清理 list
            //批量插入
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("所有数据解析完成！");
    }
}
